package com.portal.gui.salesman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.Feedback;
import com.portal.logic.FeedbackDAO;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;
import com.portal.logic.SerializationUtil;

public class AddFeedbackToSaleByCustomerScr extends JFrame {

	private JPanel contentPane;
	private JTextField txtFeedback;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddFeedbackToSaleByCustomerScr frame = new AddFeedbackToSaleByCustomerScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddFeedbackToSaleByCustomerScr() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Select Product");
		lblNewLabel.setBounds(227, 32, 105, 16);
		contentPane.add(lblNewLabel);
		
		final ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection()) ;
		List<Product> sales = productDAO.selectAll();
		final JComboBox cmbProducts = new JComboBox();
		Iterator<Product> iterator1 = sales.iterator();
		while (iterator1.hasNext()) {
			Product product = (Product) iterator1.next();
			cmbProducts.addItem(product.getName());
		}
		cmbProducts.setBounds(344, 29, 76, 22);
		contentPane.add(cmbProducts);
		
		JLabel lblSelectCustomer = new JLabel("Select Customer");
		lblSelectCustomer.setBounds(12, 32, 94, 16);
		contentPane.add(lblSelectCustomer);
		
		final CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection()) ;
		List<Customer> customers = customerDAO.selectAll();
		final JComboBox cmbCustomers = new JComboBox();
		Iterator<Customer> iterator2 = customers.iterator();
		while (iterator2.hasNext()) {
			Customer customer = (Customer) iterator2.next();
			cmbCustomers.addItem(customer.getName());
		}
		cmbCustomers.setBounds(118, 29, 69, 22);
		contentPane.add(cmbCustomers);
		
		JLabel lblWriteFeedback = new JLabel("Write Feedback");
		lblWriteFeedback.setBounds(50, 117, 110, 16);
		contentPane.add(lblWriteFeedback);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO
				String feed = (String)txtFeedback.getText();
				String selectedCustomer = (String)cmbCustomers.getSelectedItem();
				String selectedProduct = (String)cmbProducts.getSelectedItem();
				
				CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection());
				Customer customer = customerDAO.selectByName(selectedCustomer);
				
				ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
				Product product = productDAO.selectByName(selectedProduct);
		
				FeedbackDAO feedbackDAO = new FeedbackDAO(ConnectionManager.getConnection());
				Feedback feedback = new Feedback();
				feedback.setId(feedbackDAO.selectMaxId()+1);
				feedback.setFeedBack(feed);
				feedback.setProductId(product.getId());
				feedback.setCustomerId(customer.getId());
				
				try {
					SerializationUtil.serialize(feedback, "c:\\feedback.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				
				feedbackDAO.insert();
				JOptionPane.showMessageDialog(null, "New Feedback was added!");
				
			}
		});
		btnSubmit.setBounds(255, 143, 97, 25);
		contentPane.add(btnSubmit);
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(12, 180, 97, 25);
		contentPane.add(btnBack);
		
		txtFeedback = new JTextField();
		txtFeedback.setBounds(249, 114, 116, 22);
		contentPane.add(txtFeedback);
		txtFeedback.setColumns(10);
	}

}
